package org.redemptionhost.web.pages;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.protocol.http.WebRequest;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class LoginLink extends ExternalLink {
	/**
	 * ExternalLink that redirects to the login or logout page depending of the
	 * user's current login status.
	 */
	private static final long serialVersionUID = 1L;

	public LoginLink(final String id) {
	    super(id, getURL());
	    add(new Label("label", new LoginLabelModel()));
	}

	public static String getURL() {
		final RequestCycle requestCycle = RequestCycle.get();

	    // Get the URL of this app to redirect to it
	    final WebRequest request = (WebRequest) requestCycle.getRequest();
	    final HttpServletRequest req = request.getHttpServletRequest();
	    final String appUrl = req.getRequestURL().toString();

	    // Create the login/logout page URL with with redirect tho this app
	    final String targetUrl = getUserService().isUserLoggedIn() ? loginUrl(appUrl)
	                : logoutUrl(appUrl);

	    return targetUrl;

	}

	private static String logoutUrl(final String appUrl) {
	    return getUserService().createLoginURL(appUrl);
	}

	private static String loginUrl(final String appUrl) {
	    return getUserService().createLogoutURL(appUrl);
	}

	private static UserService getUserService() {
	    return UserServiceFactory.getUserService();
	}

	/** Returns "Login"/"Logout/ depending of the current user's login status. */
	private final class LoginLabelModel extends AbstractReadOnlyModel<String> {
	    private static final long serialVersionUID = 1L;

	    @Override
	    public String getObject() {
	       return getUserService().isUserLoggedIn() ? "Logout" : "Login";
	    }
	}
}
